/*
 */

package com.jams.goodox.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.jams.goodox.R;
import com.jams.goodox.adapter.viewHolder.PictureHolder;
import com.jams.goodox.entity.AnnexBean;
import com.jams.goodox.utils.CollectionUtils;
import com.jams.goodox.utils.DensityUtil;

import java.util.List;

/**
 * Created by GJJ on 2016/6/28.
 * ***
 * Tel: ***
 */

public class PictureGroupAdapter extends BaseAdapter {
    private Context mContext;
    private List<AnnexBean> imageList;
    private int width;
    private int gap;
    private int maxCounts;

    /**
     *
     * @param images 图片列表
     * @param gap 除了图片之外所有的间距和（dp)
     * @param maxCounts 最大数量
     */
    public PictureGroupAdapter(Context mContext, List<AnnexBean> images, int gap, int maxCounts) {
        this.mContext = mContext;
        this.imageList = images;
        this.gap = gap;
        this.maxCounts = maxCounts;
        width = DensityUtil.getDisplayWidth(mContext);
    }

    @Override
    public int getCount() {
        if (CollectionUtils.hasData(imageList)) {//修改判空的方法
            if(imageList.size()>maxCounts){
                return maxCounts;
            }
            if (imageList.size() == 4) {
                return imageList.size() + 1;
            } else {
                return imageList.size();
            }
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (imageList.size() > position) {
            return imageList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        PictureHolder pictureHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.only_image_item, parent, false);
            pictureHolder = new PictureHolder(convertView, width,mContext);
            convertView.setTag(pictureHolder);
        } else {
            pictureHolder = (PictureHolder) convertView.getTag();
        }
        pictureHolder.setData(imageList, position,gap,maxCounts);
        return convertView;
    }

}
